package com.yshkj.payment.service.impl.operator;

import com.yshkj.payment.BeanProvider;
import com.yshkj.payment.dto.CommonJsonDto;
import com.yshkj.payment.dto.MedicinalFormDto;
import com.yshkj.payment.dto.ProducerDto;
import com.yshkj.payment.dto.SpecDto;
import com.yshkj.payment.entity.medicinal.Medicinal;
import com.yshkj.payment.entity.medicinal.Producer;
import com.yshkj.payment.entity.medicinal.Spec;
import com.yshkj.payment.repository.MedicinalRepository;
import com.yshkj.payment.repository.ProducerRepository;
import com.yshkj.payment.repository.SpecRepository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 唐旭 on 2016/10/6.
 */
public class MedicinalFormDtoUpdater {
    private MedicinalFormDto formDto;
    private transient MedicinalRepository medicinalRepository = BeanProvider.getBean(MedicinalRepository.class);
    private transient ProducerRepository producerRepository = BeanProvider.getBean(ProducerRepository.class);
    private transient SpecRepository specRepository = BeanProvider.getBean(SpecRepository.class);

    public MedicinalFormDtoUpdater(MedicinalFormDto formDto) {
        this.formDto = formDto;
    }

    public CommonJsonDto update() {
        //创建
        Medicinal medicinal;
        if (StringUtils.isEmpty(formDto.getUuid())) {
            medicinal = new Medicinal();
            formDto.update(medicinal);
        } else {
            //更新
            medicinal = medicinalRepository.findOne(formDto.getUuid());
            formDto.update(medicinal);
        }
        updateProducer(medicinal);
        clearOldSpec(medicinal);
        updateSpecList(medicinal);
        medicinalRepository.save(medicinal);
        return new CommonJsonDto(true);
    }

    private void clearOldSpec(Medicinal medicinal) {
        List<Spec> specList = medicinal.getSpecList();
        List<SpecDto> specListDto = formDto.getSpecList();
        List<Spec> clearList = new ArrayList<>();
        for (Spec spec : specList) {
            boolean exist = false;
            for (SpecDto specDto : specListDto) {
                if (spec.getUuid().equals(specDto.getUuid())) {
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                clearList.add(spec);
            }
        }
        for (Spec spec : clearList) {
            spec.setMedicinal(null);
            specList.remove(spec);
        }
    }

    private void updateSpecList(Medicinal medicinal) {
        List<SpecDto> specList = formDto.getSpecList();
        for (SpecDto specDto : specList) {
            updateSpec(specDto, medicinal);
        }
    }

    private void updateSpec(SpecDto specDto, Medicinal medicinal) {
        List<Spec> specList = medicinal.getSpecList();
        if (StringUtils.isEmpty(specDto.getUuid())) {
            Spec spec = new Spec();
            specDto.update(spec);
            specList.add(spec);
        } else {
            Spec one = specRepository.findOne(specDto.getUuid());
            specDto.update(one);
            specList.add(one);
        }
    }

    private void updateProducer(Medicinal medicinal) {
        ProducerDto producerDto = formDto.getProducer();
        if (formDto.isNewProducer()) {
            Producer producer = new Producer();
            producerDto.update(producer);
            Producer save = producerRepository.save(producer);
            medicinal.setProducer(save);
        } else {
            if (producerDto != null && !StringUtils.isEmpty(producerDto.getUuid())) {
                Producer one = producerRepository.findOne(producerDto.getUuid());
                medicinal.setProducer(one);
            }
        }
    }
}
